package yxy.game.pm2.active.module;

import org.joda.time.DateTime;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.actfundreward;
import yxy.cherry.data.bean.task;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.active.module.lockorder.LockOrderInfo;
import yxy.game.pm2.active.module.subordinateclass.barrierfund.BarrierFund;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.PlayerModule;
import yxy.game.pm2.bean.task.Task;
import yxy.game.pm2.module.tip.TipHandle;
import yxy.game.pm2.module.tip.TipType;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class ActiveModule_关卡基金 extends PlayerModule {

	//bean ----------------------------------------------------
	public static final int acttype = 113;
	
	private BarrierFund bf113;	//acttype_修炼塔基金
	private BarrierFund bf114;	//acttype_征战基金
	private BarrierFund bf115;	//acttype_扫荡基金
	private HashMap<Integer, DateTime> lockordermap = new HashMap<Integer, DateTime>();		//锁单 key：goodsid value:time
	
	public BarrierFund getBf113() {
		return bf113;
	}
	public void setBf113(BarrierFund bf113) {
		this.bf113 = bf113;
	}
	public BarrierFund getBf114() {
		return bf114;
	}
	public void setBf114(BarrierFund bf114) {
		this.bf114 = bf114;
	}
	public BarrierFund getBf115() {
		return bf115;
	}
	public void setBf115(BarrierFund bf115) {
		this.bf115 = bf115;
	}
	public HashMap<Integer, DateTime> getLockordermap() {
		return lockordermap;
	}
	public void setLockordermap(HashMap<Integer, DateTime> lockordermap) {
		this.lockordermap = lockordermap;
	}
	
	//func ----------------------------------------------------
	


	/**
	 * 根据type获取关卡基金信息
	 * @param type
	 * @return
	 */
	public BarrierFund getbfbytype(int type) {
		if(type == acttype_修炼塔基金) {
			return getBf113();
		}
		
		if(type == acttype_征战基金) {
			return getBf114();
		}
		
		if(type == acttype_扫荡基金) {
			return getBf115();
		}
		
		return null;
	}
	
	/**
	 * 根据商品id获取关卡基金信息
	 * @param goodsid
	 * @return
	 */
	public BarrierFund getbfbygoodsid(int goodsid) {
		
		ArrayList<BarrierFund> bfs = new ArrayList<BarrierFund>();
		if(getBf113() != null) {
			bfs.add(getBf113());			
		}
		if(getBf114() != null) {
			bfs.add(getBf114());			
		}
		if(getBf115() != null) {
			bfs.add(getBf115());			
		}

		for(BarrierFund bf : bfs) {
			if(bf.getGoodsid() == goodsid) {
				return bf;
			}
		}
		
		return null;
	}
	
	/**
	 * 根据type设置关卡基金信息
	 * @param type
	 * @return
	 */
	public void setbfbytype(int type, BarrierFund bf) {
		if(type == acttype_修炼塔基金) {
			setBf113(bf);
			return;
		}
		
		if(type == acttype_征战基金) {
			setBf114(bf);
			return;
		}
		
		if(type == acttype_扫荡基金) {
			setBf115(bf);
			return;
		}
	}
	
	/**
	 * 能否购买某个基金
	 * @param goodsid
	 * @return
	 */
	public int canbuy(int goodsid) {
		if(getLockordermap().containsKey(goodsid)) {
//			return cannotget;
			return DrawStatus.cannotget.getStatus();
		}
		
		BarrierFund bf = getbfbygoodsid(goodsid);
		if(bf == null) {
//			return canget;
			return DrawStatus.canget.getStatus();
		}
		
		
		if(bf != null) {
			if(bf.getBuytime() != null) {
//				return hadget;
				return DrawStatus.hadget.getStatus();
			}
		}
		
//		return canget;
		return DrawStatus.canget.getStatus();
	}
	
	public static final int acttype_修炼塔基金 = 113;
	public static final int acttype_征战基金 = 114;
	public static final int acttype_扫荡基金 = 115;

//	public static final int canget = 1;
//	public static final int hadget = 2;
//	public static final int cannotget = 3;
	
	/** 创建ActiveModule_关卡基金
	 * @param pcode
	 */
	public static ActiveModule_关卡基金 create(String pcode) {
		ActiveModule_关卡基金 am = new ActiveModule_关卡基金();
		am.setPlayerCode(pcode);
		
		Map<String, actfundreward> execlmap = ActDataCenter.getAllData(actfundreward.class, pcode);
		
		ArrayList<Integer> groups = new ArrayList<Integer>();
		groups.add(acttype_修炼塔基金);
		groups.add(acttype_征战基金);
		groups.add(acttype_扫荡基金);
		
		for(int group : groups) {
			ArrayList<actfundreward> execls = new ArrayList<actfundreward>();
			for (actfundreward execl : execlmap.values()) {
				if(execl.getGroup() == group) {
					execls.add(execl);
				}
			}
			
			int goodsid = execls.get(0).getChargeid();
			
			BarrierFund bf = new BarrierFund();
//			bf.setBuytime(DateTime.now());
			bf.setGoodsid(goodsid);
			bf.setGroup(group);
			for(actfundreward execl : execls) {
				int taskid = execl.getTaskid();
				
				task taskexecl = DataCenter.getData(taskid, task.class);
				Task taskbean = Task.create(pcode, taskexecl);
				bf.getIdcodemap().put(execl.getId(), taskbean.getCode());
			}
			am.setbfbytype(group, bf);
		}
		
		am.set();
		return am;
	}

	public void update() {
		//刷新锁单时间
		HashMap<Integer, DateTime> map = getLockordermap();
		if(map.size() == 0) {
			return;
		}
		
		ArrayList<Integer> goodsids = new ArrayList<Integer>();
		for(int goodsid : map.keySet()) {
			DateTime locktime = map.get(goodsid);
			
			if(DateTime.now().getMillis() - locktime.getMillis() > LockOrderInfo.lockTime()) {
				goodsids.add(goodsid);
			}
		}
		
		boolean save = false;
		for(int goodsid : goodsids) {
			getLockordermap().remove(goodsid);
			save = true;
		}
		
		if(save) {
			set();
		}
	}

	/**
	 * 处理购买
	 * @param goodsid
	 */
	public void dealbuy(int goodsid) {

		BarrierFund bf = getbfbygoodsid(goodsid);
		bf.setBuytime(DateTime.now());
		
		getLockordermap().remove(goodsid);
		set();
	}
	
	//锁单
	public void lockorder(int goodsid) {
		getLockordermap().put(goodsid, DateTime.now());
		set();
	}
	
	//解锁
	public void unlockorder(int goodsid) {
		getLockordermap().remove(goodsid, DateTime.now());
		set();
	}
	
	// redis ---------------------------------------------------------

	static private String key(String playerCode) {
		return String.format("PLAYER:%s:ACTIVE:%S", playerCode, acttype);
	}

	public void set() {
		try {
			setv(key(getPlayerCode()), this);
			check();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取 ActiveModule_关卡基金
	 * 
	 * @param playerCode
	 * @return
	 */
	static public ActiveModule_关卡基金 get(String playerCode, Boolean isNewPlayer) {
		try {
			ActiveModule_关卡基金 module = getv(key(playerCode));
			if (module == null) {

				//新用户不需要去查数据库
				if(!isNewPlayer){
					module = MySQLCacheDAO.getV(key(playerCode), ActiveModule_关卡基金.class);
					if(module != null){module.set();}//同步到redis
				}

				if (module == null) {
					module = create(playerCode);
				}
			}

			module.update();
			module.check();
			return module;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/** 查看是否有可领取奖励 控制红点 */
	public void check() {
		new ActOpenHandle().updateActOpen(getPlayerCode(), acttype_修炼塔基金, OpenType.开启);
		new ActOpenHandle().updateActOpen(getPlayerCode(), acttype_征战基金, OpenType.开启);
		new ActOpenHandle().updateActOpen(getPlayerCode(), acttype_扫荡基金, OpenType.开启);
		
		if(getBf113() != null) {
			boolean rewardtiptype = false;
			HashMap<Integer, String> map = getBf113().getIdcodemap();
			for(Integer id : map.keySet()) {
				String code = map.get(id);
				Task task = Task.get(getPlayerCode(), code);
				if(task!=null &&task.isFinish() && (!getBf113().getDrawids().contains(id) || (!getBf113().getPaydrawids().contains(id) && getBf113().getBuytime() != null))) {
					rewardtiptype = true;
					new TipHandle().updateTip(getPlayerCode(), TipType.活动_修炼塔基金奖励, true);
				}
			}
			
			if(!rewardtiptype) {
				new TipHandle().updateTip(getPlayerCode(), TipType.活动_修炼塔基金奖励, false);
			}
		}

		if(getBf114() != null) {
			boolean rewardtiptype = false;
			HashMap<Integer, String> map = getBf114().getIdcodemap();
			for(Integer id : map.keySet()) {
				String code = map.get(id);
				Task task = Task.get(getPlayerCode(), code);
				if(task!=null &&task.isFinish() && (!getBf114().getDrawids().contains(id) || (!getBf114().getPaydrawids().contains(id) && getBf114().getBuytime() != null))) {
					rewardtiptype = true;
					new TipHandle().updateTip(getPlayerCode(), TipType.活动_征战基金奖励, true);
				}
			}
			
			if(!rewardtiptype) {
				new TipHandle().updateTip(getPlayerCode(), TipType.活动_征战基金奖励, false);
			}
		}

		if(getBf115() != null) {
			boolean rewardtiptype = false;
			HashMap<Integer, String> map = getBf115().getIdcodemap();
			for(Integer id : map.keySet()) {
				String code = map.get(id);
				Task task = Task.get(getPlayerCode(), code);
				if(task!=null &&task.isFinish() && (!getBf115().getDrawids().contains(id) || (!getBf115().getPaydrawids().contains(id) && getBf115().getBuytime() != null))) {
					rewardtiptype = true;
					new TipHandle().updateTip(getPlayerCode(), TipType.活动_扫荡基金奖励, true);
				}
			}
			
			if(!rewardtiptype) {
				new TipHandle().updateTip(getPlayerCode(), TipType.活动_扫荡基金奖励, false);
			}
		}
	}

	//存储到数据库的方法
	public static void saveToDB(String playerCode) {
		try {
			String key = key(playerCode);
			if (touchBucket(key).isExists()) {
				ActiveModule_关卡基金 activeModule_关卡基金 = getv(key(playerCode));
				MySQLCacheDAO.setV(key, activeModule_关卡基金);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	//从redis移走数据的操作
	public static void removeAllFromRedis(String playerCode) {
		try {
			touchBucket(key(playerCode)).delete();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}
	// lock==========================================================================

	@Override
	public String lockKey() {
		return String.format("lock:%s", key(getPlayerCode()));
	}

}
